package com.cheetah.start.module.system.controller;

import com.baomidou.mybatisplus.solon.plugins.pagination.Page;
import com.cheetah.start.common.base.query.PageQuery;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.module.system.domain.SysSet;
import com.cheetah.start.module.system.service.SysSetService;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NotBlank;

/**
 * 系统设置控制器
 *
 * @author xjh
 * @date 2022/04/22
 */
@Slf4j
@Controller
@Mapping("/set")
public class SysSetController extends ResultController {

    @Inject
    SysSetService sysSetService;

    @Get()
    public ModelAndView listView() {
        return jumpPage("/set/list");
    }

    @Get()
    @Mapping("/add")
    public ModelAndView addView() {
        return jumpPage("/set/add");
    }

    @Get()
    @Mapping("/edit")
    public ModelAndView edit(ModelAndView modelAndView, @NotBlank(message = "key不能为空") String setKey) {
        SysSet byKey = sysSetService.getByKey(setKey, false);
        modelAndView.put("sysSet", byKey);
        modelAndView.view("/set/edit");
        return modelAndView;
    }


    @Get()
    @Mapping("/list")
    public ResultTable list(PageQuery pageQuery, SysSet sysSet) {
        Page<SysSet> pageInfo = sysSetService.pageList(pageQuery, sysSet);
        return pageTable(pageInfo.getRecords(), pageInfo.getTotal());
    }


    @Post()
    @Mapping("/save")
    public Result<Boolean> save(SysSet sysSet) {
        return success(sysSetService.saveOrUpdateByKey(sysSet));
    }

    @Get
    @Mapping("/getSome")
    public Result getSome(String r) {
        return success(sysSetService.getByKey(r, false));
    }


    @Post
    @Mapping("/delete")
    public Result deleteByKey(@NotBlank(message = "key不能为空") String key) {
        return decide(sysSetService.removeByKey(key));
    }

}
